package edu.wfu.preparedDishes.commodity.service.impl;

import edu.wfu.preparedDishes.commodity.utils.UUIDUtil;
import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.Query;


import edu.wfu.preparedDishes.user.dao.UserDao;
import edu.wfu.preparedDishes.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.wfu.preparedDishes.commodity.dao.MerchantDao;
import edu.wfu.preparedDishes.commodity.entity.MerchantEntity;
import edu.wfu.preparedDishes.commodity.service.MerchantService;


@Service("merchantService")
public class MerchantServiceImpl extends ServiceImpl<MerchantDao, MerchantEntity> implements MerchantService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private MerchantDao merchantDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MerchantEntity> page = this.page(
                new Query<MerchantEntity>().getPage(params),
                new QueryWrapper<MerchantEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void add(MerchantEntity merchant) {
        UserEntity userEntity= new UserEntity();
        userEntity.setUserName(UUIDUtil.get12UUID());
        userEntity.setUserRole(2);
        userDao.insert(userEntity);
        this.save(merchant);
    }

    @Override
    public MerchantEntity getMerchantBySpuId(String spuId) {
        QueryWrapper<MerchantEntity> wrapper = new QueryWrapper<MerchantEntity>()
                .eq("spu_id",spuId);
        return merchantDao.selectOne(wrapper);
    }
    @Override
    public MerchantEntity getMerchantByUserId(Long id) {
        QueryWrapper<MerchantEntity> wrapper=new QueryWrapper<MerchantEntity>()
                .eq("user_id",id);
        MerchantEntity merchantEntity = merchantDao.selectOne(wrapper);
        return merchantEntity;
    }
}